package com.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.HashMap;
import java.util.Map;

public class HttpRequest implements Request {

	private Socket socket;

	public HttpRequest(Socket socket) {
		this.socket = socket;
	}

	Map<String, String> map = new HashMap<String, String>();
	String string;

	@Override
	public Map<String, String> getHeaders() {
		BufferedReader buffer = null;
		try {
			InputStream inputStream = socket.getInputStream();
			buffer = new BufferedReader(new InputStreamReader(inputStream));
			String readLine = buffer.readLine();
			string = readLine;
			while ((readLine = buffer.readLine()) != null
					&& readLine.length() > 0) {
				int indexOf = readLine.indexOf(":", 0);
				if (indexOf != -1) {
					String substring = readLine.substring(0, indexOf);
					int indexOf2 = readLine.indexOf(" ", 0);
					String substring2 = readLine.substring(indexOf2 + 1);
					map.put(substring, substring2);
				} else {
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public String getHeader(String name) {
		if (map.get(null) == null) {
			getHeaders();
		}
		return map.get(name);
	}

	@Override
	public String getMethod() {
		if (string == null) {
			getHeaders();
		}
		return string.substring(0, string.indexOf(" "));
	}

	@Override
	public String getQueryString() {
		if (string == null) {
			getHeaders();
		}
		String url = getRequestURL();
		int indexOf = url.indexOf("?");
		if (indexOf < 0) {
			return null;
		}
		return url.substring(indexOf + 1);
	}

	@Override
	public String getRequestURL() {
		if (string == null) {
			getHeaders();
		}
		return string.substring(string.indexOf(" ") + 1,
				string.indexOf(" HTTP"));
	}

	@Override
	public String getParameter(String key) {
		if (string == null) {
			getHeaders();
		}
		Map<String, String> mapKey = new HashMap<String, String>();
		String vluse = getQueryString();
		if (vluse == null) {
			return null;
		}
		// GET /suer?name=wu&age=18&ghg=4/bowen.html HTTP/1.1
		String string = vluse;
		while (true) {
			int indexOf = string.indexOf("&");
			if (indexOf > 0) {
				String substring = string.substring(0, indexOf);
				int indexOf2 = substring.indexOf("=");
				String substring2 = substring.substring(0, indexOf2);
				String substring3 = substring.substring(indexOf2 + 1);
				mapKey.put(substring2, substring3);
				string = string.substring(indexOf + 1);
			} else {
				int indexOf1 = string.indexOf("=");
				String substring21 = string.substring(0, indexOf1);
				String substring31 = string.substring(indexOf1 + 1);
				mapKey.put(substring21, substring31);
				break;
			}
		}
		return mapKey.get(key);
	}

	@Override
	public String getRemoteAddress() {
		SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress();
		return remoteSocketAddress.toString().substring(1,
				remoteSocketAddress.toString().indexOf(":"));
	}

	@Override
	public String getRemotePort() {
		SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress();
		return remoteSocketAddress.toString().substring(
				remoteSocketAddress.toString().indexOf(":"));
	}

	@Override
	public InputStream getInputStream() {
		try {
			return socket.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	@Override
	public String getContenxtPath() {
		return string.substring(string.indexOf("/"), string.indexOf(" HTTP"));
	}
	public String getURL(){
		if (string == null) {
			getHeaders();
		}
		String url = getRequestURL();
		int indexOf = url.indexOf("?");
		if (indexOf<0) {
			int lastIndexOf = url.lastIndexOf("/"+1);
			return url.substring(lastIndexOf);
		}
		int lastIndexOf = url.lastIndexOf("/");
		if (lastIndexOf<0) {
			return null;
		}
		String substring = url.substring(lastIndexOf+1, indexOf);
		return substring;
		
	}

	@Override
	public String getIp() {
		if (string == null) {
			getHeaders();
		}
		String ip=map.get("Host");
		
		if (ip.contains(":")) {
			return ip.substring(ip.indexOf(":"));
		}else{
			return ip;
		}
	}
}
